package com.demo.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author liangliang
 */
@Controller
@RequestMapping("hello")
public class LoginController {

    // 登录
    @RequestMapping("login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                        Model model, HttpSession session) {

        if ("admin".equals(username) && "123456".equals(password)) {
            session.setAttribute("user", username);
            model.addAttribute("user", username);
            return "redirect:/helloword";
        }
        model.addAttribute("msg", "用户名或密码不正确");
        return "login";
    }

    // 登出
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }
}
